/**
* Created by 周翔 on 2017/3/30.
*/

<template>
  <div>

    <div class="btn-group-wrapper bottom">
      <el-button-group>
        <el-button type="primary" icon="plus" @click="addHandler"/>
        <el-button type="primary" icon="edit" @click="editHandler"/>
        <el-button type="primary" icon="delete" @click="delHandler"/>
      </el-button-group>
    </div>

    <el-table ref="operationContentTable" :data="getOperationContent" height="300" border :stripe="true" :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler" current-row-key="id" style="width: 100%">
      <el-table-column type="index" width="100"/>
      <el-table-column prop="title" width="150" label="名称" />
      <el-table-column prop="subTitle" label="副标题" width="150" />
      <el-table-column prop="blockName" width="150" label="区块"/>
      <el-table-column prop="status" width="150" label="状态" :formatter="formatterStatusHandler"/>
      <el-table-column prop="tag" label="标签" width="150"/>
      <el-table-column prop="style" label="颜色值" width="150"/>
      <el-table-column prop="priority" width="150" label="优先级"/>
      <el-table-column prop="indicantType" width="150" label="指标类型" :formatter="formatterIndicantTypeHandler"/>
      <el-table-column prop="url" label="链接地址" width="150"/>
      <el-table-column prop="version" label="版本" width="150"/>
      <el-table-column prop="createTime" width="200" label="创建时间"/>
      <el-table-column prop="lastUpdatedTime" label="最近更新时间" width="200"/>
      <el-table-column prop="indicantDescription" label="指标备注" width="150"/>
      <el-table-column inline-template :context="_self" label="操作" width="100" fixed="right">
      <span>
        <el-button :type="row.status === '1'?'danger':'info'" size="small"
                   @click="onlineHandler($index,row)">{{row.status
          === '1'?'上线':'下线'}}</el-button>
      </span>
      </el-table-column>
    </el-table>

    <operation-content-form ref="operationContentForm" :mode="formMode" :param="currentRecord"/>
  </div>
</template>

<script type="text/babel">
  import { mapActions, mapGetters } from 'vuex'
  import operationContentStore from './bll/operationContentStore'
  import store from '../../store'
  (!store.state.operationContent) && store.registerModule('operationContent', operationContentStore)
  import operationContentForm from './operationContentForm.vue'

  export default{
    data() {
      return {
        formMode: null,
        currentRecord: null,
        indicantTypes: {
          '1W': '上周',
          '1LW': '最近一周',
          '1M': '一个月',
          '3M': '三个月',
          '6M': '半年',
          '1Y': '一年',
          '2Y': '两年',
          '3Y': '三年',
          '5Y': '五年',
          '10Y': '十年'
        }
      }
    },

    computed: {
      ...mapGetters(['getOperationContent'])
    },

    components: {operationContentForm},

    mounted() {
      this.initPage();
    },
    methods: {
      ...mapActions(['queryOperationContent', 'deleteOperationContent', 'onlineOperationContent']),

      initPage() {
        console.log('[operationContent Page] mounted');
        this.queryOperationContent();
      },
      addHandler() {
        this.currentRecord = null;

        // hack table again
        let selectedRow = this.$refs.operationContentTable.$el.querySelector('.current-row');
        if (selectedRow && selectedRow.classList) {
          selectedRow.classList.remove('current-row');
        }
        this.formMode = 'ADD';
        this.$refs.operationContentForm.enable();
        this.$refs.operationContentForm.clear();
      },
      editHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条数据');
          return;
        }
        this.$refs.operationContentForm.enable();
        this.formMode = 'EDIT';
      },
      delHandler() {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条数据');
          return;
        }

        this._deleteOperationContent(this.currentRecord.id)
      },
      formatterStatusHandler (row, column) {
        switch (column.property) {
          case 'status':
            switch (row.status) {
              case '0':
                return '上线';
              case '1':
                return '下线';
            }
            break;
        }
        return 'Unkown key'
      },
      formatterIndicantTypeHandler (row, column) {
        switch (column.property) {
          case 'indicantType':
            return this.indicantTypes[row.indicantType]
        }
        return 'Unkown key'
      },
      onlineHandler(index, row) {
        this.$confirm(row.status === '1' ? '你确定要上线吗?' : '你确定要下线吗?', '确定', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.onlineOperationContent(row).then(data => {
            this.$message.success(row.status === '1' ? '下线成功' : '上线成功');
          });
        });
      },
      tableRowChangeClickHandler (currentRow, oldCurrentRow) {
        console.log(currentRow);
        this.currentRecord = currentRow;
        this.$refs.operationContentForm.disable();
      },
      _deleteOperationContent(id) {
        this.$confirm('你确定要删除这条数据吗?', '确认', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteOperationContent({id}).then(data => {
            this.$message.success('数据删除成功');
            this.currentRecord = null;
            this.$refs.operationContentForm.clear();
          });
        });
      }
    }
  }
</script>
